import seriesConfig from "../../constants/echarts-config/series.json";
import dataConfig from "../../constants/data.json";

function generatorBaseSeries() {
  return {
    name: "series类型",
    node: "select",
    options: Object.keys(seriesConfig).map(key => {
      return {
        label: (seriesConfig as any)[key]?.name,
        value: key        
      }
    })
  }
}
/**
 * 生成Series系列的物料配置
 * @param series 
 * @returns 
 */
export function generatorSeriesConfig(series: Array<any>) {
  const baseSeries = generatorBaseSeries();
  return series.length > 0 ? series.map(item => {
    let current;
    if (item.type) {
      current = { type: { ...baseSeries, default: item.type }, ...(seriesConfig as any)[item.type]?.children }
    }
    !current && (current = { type: baseSeries });
    return current;
  }) : [{ type: baseSeries }];
}

/**
 * 生成数据源物料配置
 * @param dataSource 
 * @returns 
 */
export function generatorDataSourceConfig(dataSource: Array<any>) {
  const dataOrigin = Object.assign({}, dataConfig.baseOrigin, dataConfig.dataOrigin)
  const dataOriginOptions = Object.keys(dataOrigin).map(key => ({ label: (dataOrigin as any)[key].name, value: key }));
  return {
    dataSource: {
      name: "数据源配置",
      node: "array",
      length: dataSource.length || 1,
      childrenList: dataSource.length > 0 ? dataSource.map(item => {
        return {
          origin: {
            ...dataConfig.config.origin,
            options: dataOriginOptions,
            default: item.origin
          },
          ...(dataOrigin as any)[item.origin]?.children,
          convertFunc: {
            ...dataConfig.config.convertFunc,
            default: item.convertFunc
          }
        }
      }) : [{ origin: { ...dataConfig.config.origin, options: dataOriginOptions }, convertFunc: dataConfig.config.convertFunc }],
      default: dataSource
    }
  }
}

/** 转化地图数据源配置 */
function transformGeoMapConfig(baseConfig: any, geoMap: Array<any>) {
  const dataOrigin = Object.assign({}, dataConfig.baseOrigin, dataConfig.mapOrigin);
  const dataOriginOptions = Object.keys(dataOrigin).map(key => ({ label: (dataOrigin as any)[key].name, value: key }));
  return geoMap.length > 0 ? geoMap.map(item => {
    return {
      ...baseConfig,
      origin: {
        ...baseConfig?.origin,
        options: dataOriginOptions,
        default: item.origin
      },
      ...(dataOrigin as any)[item.origin]?.children
    }
  }) : [{
    ...baseConfig,
    origin: {
      ...baseConfig?.origin,
      options: dataOriginOptions,
    }
  }]
}
/**
 * 转化物料基础数据源配置信息
 */
export function transformDataSourceBaseConfig(baseConfig: any, dataSource: any) {
  /* 针对geoMap进行改造 */
  if (baseConfig?.geoMap) {
    const { childrenList } = baseConfig.geoMap || {};
    baseConfig.geoMap.childrenList = transformGeoMapConfig(childrenList?.[0], dataSource?.geoMap || [])
  }
  return baseConfig;
}